package com.ruoyi.api.controller.deal;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.api.annotation.CurrentUser;
import com.ruoyi.api.annotation.IgnoreAuth;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.deal.bo.HyDealGoodsAddBo;
import com.ruoyi.deal.bo.HyDealGoodsQueryBo;
import com.ruoyi.deal.bo.HyDealOrderQueryBo;
import com.ruoyi.deal.domain.HyDealOrder;
import com.ruoyi.deal.service.IHyDealGoodsService;
import com.ruoyi.deal.service.IHyDealOrderService;
import com.ruoyi.deal.vo.HyDealGoodsVo;
import com.ruoyi.system.domain.HyUser;
import com.ruoyi.system.service.IHyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品api接口
 * @author lixin
 * @date 2021-06-18 20:57
 */
@RestController
@RequestMapping("/api/order")
public class ApiOrderController extends BaseController {

	@Autowired
	private IHyDealOrderService dealOrderService;

	@Autowired
	private IHyUserService userService;

	/**
	 * 创建订单
	 * @param user
	 * @param add
	 * @return
	 */
	@PostMapping("/create")
	public AjaxResult createOrder(@CurrentUser HyUser user, @RequestBody HyDealOrder add) {

		Map<String, Object> resultObj = new HashMap<String, Object>(1);
		resultObj.put("orderId", dealOrderService.createOrder(user, add));
		return AjaxResult.success(resultObj);
	}

	/**
	 * 获取订单详细信息
	 */
	@GetMapping("/{orderId}")
	public AjaxResult<HyDealOrder> getInfo(@PathVariable("orderId") Long orderId) {
		return AjaxResult.success(dealOrderService.queryById(orderId));
	}

	/**
	 * 查询我的买入订单列表
	 */
	@GetMapping("/buyerList")
	public TableDataInfo<HyDealOrder> buyerList(@CurrentUser HyUser user, HyDealOrderQueryBo bo) {
		bo.setBuyerId(user.getUserId());
		TableDataInfo<HyDealOrder> tableDataInfo = dealOrderService.queryPageList(bo);
		List<HyDealOrder> rows = tableDataInfo.getRows();
		for (HyDealOrder row : rows) {
			row.setQqnumber(userService.getById(row.getSellerId()).getQqnumber());
		}
		tableDataInfo.setRows(rows);
		return tableDataInfo;
	}

	/**
	 * 查询我的卖出订单列表
	 */
	@GetMapping("/sellerList")
	public TableDataInfo<HyDealOrder> sellerList(@CurrentUser HyUser user, HyDealOrderQueryBo bo) {
		bo.setSellerId(user.getUserId());
		TableDataInfo<HyDealOrder> tableDataInfo = dealOrderService.queryPageList(bo);
		List<HyDealOrder> rows = tableDataInfo.getRows();
		for (HyDealOrder row : rows) {
			row.setQqnumber(userService.getById(row.getBuyerId()).getQqnumber());
		}
		tableDataInfo.setRows(rows);
		return tableDataInfo;
	}

	/**
	 * 取消订单
	 * @param user 操作用户
	 * @param orderId
	 * @return
	 */
	@PutMapping("/cancel/{orderId}")
	public AjaxResult orderCancel(@CurrentUser HyUser user, @PathVariable("orderId") Integer orderId) {
		return toAjax(dealOrderService.orderCancel(user, orderId));
	}


	/**
	 * 确认订单
	 * @param user 操作用户
	 * @param orderId
	 * @return
	 */
	@PutMapping("/confirm/{orderId}")
	public AjaxResult orderConfirm(@CurrentUser HyUser user, @PathVariable("orderId") Integer orderId) {
		return toAjax(dealOrderService.orderConfirm(user, orderId));
	}


}
